<?php

namespace Admin\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\Form\Annotation\AnnotationBuilder;
use Zend\View\Model\ViewModel;
use Admin\Form\LoginForm;
use Admin\Model\DBO\UserDBO;

class IndexController extends AbstractActionController {

    protected $form;
    protected $storage;
    protected $authservice;

    public function getAuthService() {
        if (!$this->authservice) {
            $this->authservice = $this->getServiceLocator()
                    ->get('AuthService');
        }

        return $this->authservice;
    }

    public function getSessionStorage() {
        if (!$this->storage) {
            $this->storage = $this->getServiceLocator()
                    ->get('Admin\Model\MyAuthStorage');
        }

        return $this->storage;
    }

    public function getForm() {
        if (!$this->form) {
            $user = new LoginForm();
            $builder = new AnnotationBuilder();
            $this->form = $builder->createForm($user);
        }

        return $this->form;
    }

    public function loginAction() {
//         if already login, redirect to success page
        if ($this->getAuthService()->hasIdentity()) {
            return $this->redirect()->toRoute('success');
        }
        $viewModel = new ViewModel();
        $form = $this->getForm();
        $viewModel->setTerminal(true);
        $viewModel->setVariables(array(
            'form' => $form,
            'messages' => $this->flashmessenger()->getMessages()
        ));

        return $viewModel;
    }

    public function authenticateAction() {
        $form = $this->getForm();
        $redirect = 'admin';
        $request = $this->getRequest();
        if ($request->isPost()) {
            $form->setData($request->getPost());
            if ($form->isValid()) {
                //check authentication...

                $this->getAuthService()->getAdapter()
                        ->setIdentity($request->getPost('userName'))
                        ->setCredential($request->getPost('password'));
                $result = $this->getAuthService()->authenticate();
                foreach ($result->getMessages() as $message) {
                    //save message temporary into flashmessenger
                    $this->flashmessenger()->addMessage($message);
                }
                if ($result->isValid()) {
                    $roleId = $this->getRole();
                    $redirect = 'success';
                    // lay roleID
                    //check if it has rememberMe :
                    if ($request->getPost('rememberme') == 1) {
                        $this->getSessionStorage()
                                ->setRememberMe(1);
                        //set storage again
                        $this->getAuthService()->setStorage($this->getSessionStorage());
                    }
                    $this->getAuthService()->setStorage($this->getSessionStorage());
                    $this->getAuthService()->getStorage()->write($request->getPost('userName'));
                }
            }
        }

        return $this->redirect()->toRoute($redirect);
    }

    public function logoutAction() {
        if ($this->getAuthService()->hasIdentity()) {
            $this->getSessionStorage()->forgetMe();
            $this->getAuthService()->clearIdentity();
            $this->flashmessenger()->addMessage("You've been logged out");
        }

        return $this->redirect()->toRoute('admin');
    }

    public function getRole() {
        // Neu chua dang nhap thi quay tro ve lai trang dang nhap
        if (!$this->getAuthService()->hasIdentity()) {
            return $this->redirect()->toRoute('admin');
        }
        $identity = $this->getAuthService()->getIdentity();
        $objectUser = new UserDBO($this->getServiceLocator());
        $userInfo = $objectUser->getUserID($identity);
        if ($userInfo['roleID'] == 4) {
            return $this->redirect()->toRoute('personal');
        }
        return $userInfo['roleID'];
    }
    /*
     * Nếu không phải quyền admin thì không được vào chức năng này
     */
    public function checkRole1() {
        $roleId = $this->getRole();
        if ($roleId != 1) {
            $this->redirect()->toRoute('success/default', array('action' => 'errorRole'));
        }
    }

    public function checkRole2() {
        $roleId = $this->getRole();
        if ($roleId == 2 || $roleId == 4) {
            $this->redirect()->toRoute('success/default', array('action' => 'errorRole'));
        }
    }

    public function checkRole3() {
        $roleId = $this->getRole();
        if ($roleId == 3 || $roleId == 4) {
            $this->redirect()->toRoute('success/default', array('action' => 'errorRole'));
        }
    }

}